package com.zh.work.service.platform.trans.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zh.work.dao.platform.trans.OrderHistoryMapper;
import com.zh.work.dao.platform.trans.QrCodeOrderMapper;
import com.zh.work.entity.trans.QrCodeOrderHistoryEntity;
import com.zh.work.entity.trans.QrCodeOrderEntity;
import com.zh.work.service.platform.trans.QrCodeOrderService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description:
 * @Author: Li.HuiNeng
 * @Create in: 2019-09-09 16:29
 * @Version: 1.0.1
 */
@Service
public class QrCodeOrderServiceImpl extends ServiceImpl<QrCodeOrderMapper, QrCodeOrderEntity> implements QrCodeOrderService {
    @Autowired
    private QrCodeOrderMapper qrCodeOrderMapper;

    @Autowired
    private OrderHistoryMapper orderHistoryMapper;


    @Override
    public IPage<QrCodeOrderEntity> selectQrCodeOrderPages(IPage<QrCodeOrderEntity> page, QrCodeOrderEntity qrCodeOrderEntity) {
        return this.qrCodeOrderMapper.selectPage(
                page,
                new QueryWrapper<QrCodeOrderEntity>().lambda()
                        .between(ObjectUtils.allNotNull(qrCodeOrderEntity.getStartDate(), qrCodeOrderEntity.getEndDate()),
                                QrCodeOrderEntity::getTransDate, qrCodeOrderEntity.getStartDate(), qrCodeOrderEntity.getEndDate())
                        .between(ObjectUtils.allNotNull(qrCodeOrderEntity.getStartTime(), qrCodeOrderEntity.getEndTime()),
                                QrCodeOrderEntity::getTransTime, qrCodeOrderEntity.getStartTime(), qrCodeOrderEntity.getEndTime())
                        .between(ObjectUtils.allNotNull(qrCodeOrderEntity.getBeginAmount(), qrCodeOrderEntity.getEndAmount()),
                                QrCodeOrderEntity::getAmount, qrCodeOrderEntity.getBeginAmount(), qrCodeOrderEntity.getEndAmount())
                        .eq(StringUtils.isNotEmpty(qrCodeOrderEntity.getBankNo()), QrCodeOrderEntity::getBankNo, qrCodeOrderEntity.getBankNo())
                        .eq(Objects.nonNull(qrCodeOrderEntity.getStatus()), QrCodeOrderEntity::getStatus, qrCodeOrderEntity.getStatus())
                        .eq(Objects.nonNull(qrCodeOrderEntity.getSettleType()), QrCodeOrderEntity::getSettleType, qrCodeOrderEntity.getSettleType())
                        .eq(StringUtils.isNotEmpty(qrCodeOrderEntity.getChannelMerchNo()), QrCodeOrderEntity::getChannelMerchNo, qrCodeOrderEntity.getChannelMerchNo())
                        .eq(Objects.nonNull(qrCodeOrderEntity.getPayType()), QrCodeOrderEntity::getPayType, qrCodeOrderEntity.getPayType())
                        .eq(StringUtils.isNotEmpty(qrCodeOrderEntity.getChannelCode()), QrCodeOrderEntity::getChannelCode, qrCodeOrderEntity.getChannelCode())
                        .like(StringUtils.isNotEmpty(qrCodeOrderEntity.getOrderNo()), QrCodeOrderEntity::getOrderNo, qrCodeOrderEntity.getOrderNo())
                        .like(StringUtils.isNotEmpty(qrCodeOrderEntity.getTraceNo()), QrCodeOrderEntity::getTraceNo, qrCodeOrderEntity.getTraceNo())
                        .like(StringUtils.isNotEmpty(qrCodeOrderEntity.getMerchName()), QrCodeOrderEntity::getMerchName, qrCodeOrderEntity.getMerchName())
                        .like(Objects.nonNull(qrCodeOrderEntity.getScanType()), QrCodeOrderEntity::getScanType, qrCodeOrderEntity.getScanType())
                        .like(Objects.nonNull(qrCodeOrderEntity.getInterType()), QrCodeOrderEntity::getInterType, qrCodeOrderEntity.getInterType())
                        .like(StringUtils.isNotEmpty(qrCodeOrderEntity.getMerchNo()), QrCodeOrderEntity::getMerchNo, qrCodeOrderEntity.getMerchNo())
                        .like(StringUtils.isNotEmpty(qrCodeOrderEntity.getMerchOrderNo()), QrCodeOrderEntity::getMerchOrderNo, qrCodeOrderEntity.getMerchOrderNo())
        );
    }

    @Override
    public IPage<QrCodeOrderHistoryEntity> selectOrderHistoryPages(IPage<QrCodeOrderHistoryEntity> page, QrCodeOrderHistoryEntity historyEntity) {
        return this.orderHistoryMapper.selectPage(
                page,
                new QueryWrapper<QrCodeOrderHistoryEntity>().lambda()
                        .between(ObjectUtils.allNotNull(historyEntity.getStartDate(), historyEntity.getEndDate()),
                                QrCodeOrderEntity::getTransDate, historyEntity.getStartDate(), historyEntity.getEndDate())
                        .between(ObjectUtils.allNotNull(historyEntity.getStartDate(), historyEntity.getEndDate()),
                                QrCodeOrderEntity::getTransTime, historyEntity.getStartDate(), historyEntity.getEndDate())
                        .between(ObjectUtils.allNotNull(historyEntity.getBeginAmount(), historyEntity.getEndAmount()),
                                QrCodeOrderEntity::getAmount, historyEntity.getBeginAmount(), historyEntity.getEndAmount())
                        .eq(Objects.nonNull(historyEntity.getPayType()), QrCodeOrderHistoryEntity::getPayType, historyEntity.getPayType())
                        .eq(StringUtils.isNotEmpty(historyEntity.getChannelCode()), QrCodeOrderHistoryEntity::getChannelCode, historyEntity.getChannelCode())
                        .eq(StringUtils.isNotEmpty(historyEntity.getBankNo()), QrCodeOrderHistoryEntity::getBankNo, historyEntity.getBankNo())
                        .eq(Objects.nonNull(historyEntity.getStatus()), QrCodeOrderHistoryEntity::getStatus, historyEntity.getStatus())
                        .eq(Objects.nonNull(historyEntity.getSettleType()), QrCodeOrderHistoryEntity::getSettleType, historyEntity.getSettleType())
                        .eq(StringUtils.isNotEmpty(historyEntity.getChannelMerchNo()), QrCodeOrderHistoryEntity::getChannelMerchNo, historyEntity.getChannelMerchNo())
                        .like(StringUtils.isNotEmpty(historyEntity.getOrderNo()), QrCodeOrderHistoryEntity::getOrderNo, historyEntity.getOrderNo())
                        .like(StringUtils.isNotEmpty(historyEntity.getTraceNo()), QrCodeOrderHistoryEntity::getTraceNo, historyEntity.getTraceNo())
                        .like(StringUtils.isNotEmpty(historyEntity.getMerchName()), QrCodeOrderHistoryEntity::getMerchName, historyEntity.getMerchName())
                        .like(Objects.nonNull(historyEntity.getScanType()), QrCodeOrderHistoryEntity::getScanType, historyEntity.getScanType())
                        .like(Objects.nonNull(historyEntity.getInterType()), QrCodeOrderHistoryEntity::getInterType, historyEntity.getInterType())
                        .like(StringUtils.isNotEmpty(historyEntity.getMerchNo()), QrCodeOrderHistoryEntity::getMerchNo, historyEntity.getMerchNo())
                        .like(StringUtils.isNotEmpty(historyEntity.getMerchOrderNo()), QrCodeOrderHistoryEntity::getMerchOrderNo, historyEntity.getMerchOrderNo())
        );
    }

    @Override
    public List<Map<String,String>> getQrCodeStatistic(QrCodeOrderEntity param) {
        return qrCodeOrderMapper.getQrCodeStatistic(param);
    }
}
